//
//  StreamEvent.swift
//
//  Created by melvyn on 2018/8/9.
//  Copyright © 2018年 NEET. All rights reserved.
//

import Foundation

enum StreamEvent {
    case upvote
    case downvote
    case present
    case comments
    case compose
    case error
    case undefine(UInt8)
    
    public init(code: UInt8) {
        switch code {
        case 0x2: self = .upvote
        case 0x3: self = .downvote
        case 0x4: self = .present
        case 0x5: self = .comments
        case 0x6: self = .compose
        case 0x11: self = .error
        default: self = .undefine(code)
        }
    }
}

extension StreamEvent: Equatable {
    public static func ==(lhs: StreamEvent, rhs: StreamEvent) -> Bool {
        switch (lhs, rhs) {
        case (.upvote, .upvote),
             (.downvote, .downvote),
             (.present, .present),
             (.comments, .comments),
             (.compose, .compose),
             (.error, .error):
             return true
        case (.undefine(let i), .undefine(let j)):
            return i == j
        default:
            return false
        }
    }
}

extension UInt8 {
    
    init(event: StreamEvent) {
        switch event {
            case .upvote: self = 0x2
            case .downvote: self = 0x3
            case .present: self = 0x4
            case .comments: self = 0x5
            case .compose: self = 0x6
            case .error: self = 0x11
            case .undefine(let i): self = i
        }
    }
}
